<?php
/**
 * Created by PhpStorm.
 * User: fairy
 * Date: 14-3-6
 * Time: 下午12:05
 */

class AdminAction extends BaseAction{
    public function index(){
        if(empty($_SESSION['email']))
        {
            $this->error("请先登录",U('/Index'));
        }
        $articles=M('Article')->select();
//        var_dump($articles);
        if(!empty($articles))
        {

            $this->assign('articles',$articles);
        }
        $this->display();


    }
    public function insert(){
        $this->display('Admin:add');
    }
    public function add(){
        if(empty($_SESSION['email']))
        {
            $this->error("请先登录",U('/Index'));
        }
        if(isset($_POST["title"]) && !empty($_POST["title"]) &&isset($_POST["content"]) && !empty($_POST["content"])) {

            $model = M('article');
            $map = array();
            $map['title'] = trim(I('post.title'));
            $map['content'] = trim(I('post.content'));

            $model->data($map)->add();
            $this->success("新增成功！",U('/Admin'));
        }
        else if(empty($_POST["title"])){
            $this->error("请输入标题");
        }else if(empty($_POST["content"])){
            $this->error("请输入正文");
        }

    }
    public function delete(){
        if(empty($_SESSION['email']))
        {
            $this->error("请先登录",U('/Index'));
        }
        if(isset($_GET["id"])) {
            $model = M('article');
            $map = array();
            $map['id'] = $_GET["id"];

            $model->where($map)->delete();

            $this->success("删除成功");
        }
        else $this->error("出错");


        $this->display();
    }
    public function edit(){
        if(empty($_SESSION['email']))
        {
            $this->error("请先登录",U('/Index'));
        }
        if(isset($_GET["id"])) {
            $model = M('article');
            $map = array();
            $map['id'] = $_GET["id"];

            $vo = $model->where($map)->find();

            $this->assign('vo', $vo);
            $this->display('Admin:edit');
        }
        else $this->error("出错");
    }
    public function update(){
        if(empty($_SESSION['email']))
        {
            $this->error("请先登录",U('/Index'));
        }
        if(isset($_POST["id"]) && !empty($_POST["title"])  && !empty($_POST["content"])) {
            $model = M('article');
            $map = array();
            $map['id'] = trim(I('post.id'));
            $data=array();
            $data['title'] = trim(I('post.title'));
            $data['content'] = trim(I('post.content'));
            $model->where($map)->data($data)->save();
            $this->success("保存成功！",U('/Admin'));
        }
        else if(empty($_POST["title"])){
            $this->error("标题不能修改为空！");
        }else if(empty($_POST["content"])){
            $this->error("正文不能修改为空！");
        }
    }
}